﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data.OracleClient;
using Maticsoft.DBUtility;
using System.Data;
using Xiaov.SmartWeb.Application.Models;

namespace Xiaov.SmartWeb.Application.SZBB
{
  public partial class SaveTable : System.Web.UI.Page
  {
    private string tableName = "", dwbm = "", nf = "", yf = "", xml = "";
    XMLVERSION modelXml = new XMLVERSION();

    protected void Page_Load(object sender, EventArgs e)
    {
      string jsonData = "";
      if (Request.Form["tableName"] != null || Request.Form["dwbm"] != null ||
        Request.Form["nf"] != null || Request.Form["yf"] != null)
      {
        tableName = Request.Form["tableName"].ToString();
        dwbm = Request.Form["dwbm"].ToString();
        nf = Request.Form["nf"].ToString();
        yf = Request.Form["yf"].ToString();
        xml = Server.UrlDecode(Request.Form["xml"].ToString());
        InitVersion();
        if (Save())
        { jsonData = "{\"save\":1}"; }
        else
        { jsonData = "{\"save\":0}"; }
      }
      else
      {
        jsonData = "{\"save\":0}";
      }
      Response.Write(jsonData);
      Response.ContentType = "text/plain";
      Response.End();
    }

    private void InitVersion()
    {
      string sql = "select * from xmlversion where year=:0 and bbbm=:1";
      OracleParameter[] parms = { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20) };
      parms[0].Value = nf;
      parms[1].Value = tableName;
      DataTable dt = DbHelperOra.Query(sql, parms).Tables[0];
      if (dt.Rows.Count == 1)
      {
        modelXml.SELECTFIELD = dt.Rows[0]["selectfield"].ToString();
        modelXml.CAPTION = dt.Rows[0]["caption"].ToString();
        modelXml.COORDINATES = dt.Rows[0]["coordinates"].ToString();
        modelXml.COLUMNCOUNT = int.Parse(dt.Rows[0]["columncount"].ToString());
        modelXml.ROWCOUNT = int.Parse(dt.Rows[0]["rowcount"].ToString());
        modelXml.Title = dt.Rows[0]["title"].ToString();
        modelXml.HIDDENCOLUMN = dt.Rows[0]["hiddencolumn"].ToString();
      }
    }

    private bool Save()
    {
      bool flag = false;
      string[] columnList = modelXml.SELECTFIELD.Split(new char[] { ',' });
      System.Text.StringBuilder sql = new System.Text.StringBuilder();
      sql.Append("begin ");
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(xml);
      XmlNode report = doc.DocumentElement;
      XmlNode worksheet = report.ChildNodes[0];
      XmlNode table = worksheet.SelectSingleNode("Table");
      foreach (XmlNode tr in table.ChildNodes)
      {
        if (tr.Name == "TR")
        {
          int rowcount = int.Parse(tr.Attributes["sequence"].Value);
          if (rowcount > (modelXml.ROWCOUNT - 1))  //当tr为数据行时
          {
            string zbbm = tr.ChildNodes[1].InnerText;
            sql.Append("update "); sql.Append(tableName); sql.Append(" set ");
            for (int i = 13; i < columnList.Length - 1; i++)
            {
              string value = tr.ChildNodes[i].InnerText;
              sql.Append(columnList[i]); sql.Append("='"); sql.Append(value); sql.Append("' ");
              if (i < columnList.Length - 2)
              { sql.Append(","); }
            }
            sql.Append(" where dwbm='"); sql.Append(dwbm);
            sql.Append("' and nf='"); sql.Append(nf);
            sql.Append("' and yf='"); sql.Append(yf);
            sql.Append("' and zbbm='"); sql.Append(zbbm);
            sql.Append("';");
          }
        }
      }
      sql.Append("null;");
      sql.Append("end;");
      flag = DbHelperOra.ExecuteSql(sql.ToString()) > 0 ? true : false; ;
      return flag;
    }
  }
}